/**
 * @Description: 鑴氭湰鏂囦欢
 * @Author: HP
 * @Date: 2017/8/30.
 */
$(function(){

    //goToTop璺濇祻瑙堝櫒椤剁鐨勮窛绂伙紝杩欎釜璺濈鍙互鏍规嵁浣犵殑闇€姹備慨鏀�
    var topDistance = 500;

    //璺濈娴忚鍣ㄩ《绔灏戣窛绂诲紑濮嬫樉绀篻oToTop鎸夐挳锛岃繖涓窛绂讳篃鍙互淇敼锛屼絾涓嶈兘瓒呰繃娴忚鍣ㄩ粯璁ら珮搴︼紝涓轰簡鍏煎涓嶅悓鍒嗚鲸鐜囩殑娴忚鍣紝鎴戝缓璁湪杩欓噷璁剧疆鍊间负1锛�
    var showDistance = 1;

    //瀹氫箟涓€涓彉閲忥紝鏂逛究鍚庨潰鍔犲叆鍦╤tml鍏冪礌鏍囩涓彃鍏ヨ繖涓猤oToTop鎸夐挳鐨勬爣绛�
    var goToTopButton = $('<div id="goToTop"><a href="javascript:;"></a></div>');
    var goBackButton = $('<a id="goBack" href="javascript:window.location.href=window.opener.document.location.href;"></a>')
    var thisTop = $(window).scrollTop() + topDistance;
    if( $('body').attr("id")!='index'){
        $('body').append(goBackButton);
    }
    //鍦╟ontainer鐨刣iv閲屾彃鍏ユ垜浠墠闈㈠畾涔夊ソ鐨刪tml鏍囩鍏冪礌
    $('body').append(goToTopButton);



    //璁剧疆goToTop鎸夐挳top鐨刢ss灞炴€у拰灞炴€у€�
    $('#goToTop').css('top' ,thisTop);

    if($(window).scrollTop() < showDistance){
        $('#goToTop').hide();
    }

    // 缁欑獥鍙ｇ粦瀹氫竴涓粴鍔ㄤ簨浠讹紝褰撶獥鍙ｆ粴鍔ㄦ潯婊氬姩鏃舵墽琛�
    $(window).scroll(function(){

        // console.log($(this).scrollTop());

        thisTop = $(this).scrollTop() + topDistance;        //鑾峰彇褰撳墠window鍚戜笂婊氬姩鐨勮窛绂�
        $('#goToTop').css('top', thisTop);                    //淇敼goToTop鎸夐挳鐨則op璺濈


        if($(this).scrollTop() > showDistance){
            $('#goToTop').fadeIn();
        }else{
            $('#goToTop').fadeOut();
        }

    });


    // 缁欐寜閽粦瀹氫竴涓猚lick浜嬩欢锛岀偣鍑绘寜閽椂锛岃繑鍥為《閮�
    $('#goToTop a').click(function(){
        $('html ,body').animate({scrollTop: 0}, 300);
        return false;
    });
});